package com.example.service.Impl;

import com.example.exception.CustomException;
import com.example.mapper.CollectionMapper;
import com.example.pojo.Collection;
import com.example.service.CollectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Resource
    private CollectionMapper collectionMapper;

    /**
     * 添加收藏
     *
     * @param collection
     */
    @Override
    public void add(Collection collection) {
        // 检查是否已收藏
        boolean exists = collectionMapper.existsByUserAndRoom(collection.getUserId(), collection.getRoomId());
        if (exists) {
            throw new CustomException("500", "用户已收藏过此房间");
        }
        collectionMapper.add(collection);
    }

    @Override
    public Boolean isCollection(Integer userId, Integer roomId) {
        // 检查是否已收藏
        boolean exists = collectionMapper.existsByUserAndRoom(userId,roomId);
        if (exists) {
           return true;
        }
        return false;
    }

    /**
     * 根据id查询收藏
     *
     * @param id
     * @return
     */
    @Override
    public Collection selectById(Integer id) {
        Collection collection = collectionMapper.selectById(id);
        return collection;
    }

    /**
     * 取消收藏
     *
     * @param
     */
    @Override
    public void deleteById(Integer userId, Integer roomId) {
        collectionMapper.deleteById(userId,roomId);
    }


    /**
     * 查询所有收藏
     *
     * @return
     */
    @Override
    public List<Collection> selectAll(Collection collection) {
        List<Collection> list = collectionMapper.selectAll(collection);
        return list;
    }


    /**
     * 分页查询
     *
     * @param pageNum  查询哪一页
     * @param pageSize 一页的大小
     * @return
     */
    @Override
    public PageInfo<Collection> selectPage(Collection collection,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Collection> list = collectionMapper.selectAll(collection);
        return PageInfo.of(list);
    }
}
